<?php

namespace C\P;

const ERROR_CODE = [
  '-1'  => '没有该用户账户',
  '-2'  => '接口密钥不正确',
  '-3'  => '短信数量不足',
  '-4'  => '手机号格式不正确',
  '-6'  => 'IP 限制',
  '-11' => '该用户被禁用',
  '-14' => '短信内容出现非法字符',
  '-21' => 'MD5接口密钥加密不正确',
  '-41' => '手机号码为空',
  '-42' => '短信内容为空',
  '-51' => '短信签名格式不正确',
  '-52' => '短信签名太长',
];


class SMSChinese
{
    private $uid;
    private $key;

  /**
   * @param $uid
   * @param $key
   */
    public function __construct($uid, $key)
    {
        $this->uid = $uid;
        $this->key = $key;
    }

  /**
   * @param $mobile
   * @param $content
   */
    public function send($mobile, $content)
    {
        $queryParams = [
        'Uid'     => $this->uid,
        'Key'     => $this->key,
        'smsMob'  => $mobile,
        'smsText' => $content,
        ];

        $queryString = http_build_query($queryParams);

        $url = 'http://utf8.api.smschinese.cn?' . $queryString;

        $response = Http::get($url, [], 10);
        if (!$response) {
            throw new \Exception('请求异常');
        }

        if ($response > 0) {
            return true;
        }

        throw new \Exception(ERROR_CODE[$response]);
    }
}
